#include "stockscontainer.h"
#include "stockinfo.h"
#include "confighelper.h"

StocksContainer::StocksContainer()
{
}

StocksContainer::~StocksContainer()
{
    for(int i = 0; i<stockList.size(); i++)
        delete stockList[i];

    stockList.clear();
}

void StocksContainer::addById(QString stockId)
{
    stockList.push_back(new StockInfo(stockId));
}

void StocksContainer::readdByIds(QStringList &s, ConfigHelper& helper)
{
    QList<StockInfo *> tmpList;

    tmpList.push_back(stockList.takeAt(0));
    tmpList.push_back(stockList.takeAt(0));

    int pos = 0;
    helper.setHasnew(false);

    for (int i = 0; i< s.size(); i++) {
        if(isExist(s[i], pos)){
            tmpList.push_back(stockList.takeAt(pos));
        }else {
            tmpList.push_back(new StockInfo(s[i]));
            helper.setHasnew(true);
        }
    }

    for(int i =0 ; i<stockList.size(); i++){
        delete stockList[i];
    }

    stockList.clear();
    stockList.append(tmpList);
    tmpList.clear();
}

bool StocksContainer::isExist(QString stockId, int& pos)
{
    for(int i = 0; i<stockList.size(); i++) {
        if(stockList[i]->getStockId() == stockId){
            pos = i;
            return true;
        }
    }

    return false;
}

StockInfo* StocksContainer::getStock(int position)
{
    return stockList[position];
}

int StocksContainer::size()
{
    return stockList.size();
}
